var ComFunJS={
  URL: "http://www.ldx178.com",
  APIURL: function (action) {
    if (action.substring(0, 1) != "/") {
      action = "/" + action;
    }
    return ComFunJS.URL + action;
  },
  FilePath: function (fileid, wh) { //文件路径
    fileid = fileid + "";
    if (fileid) {
      var url = ComFunJS.APIURL("/api1/file/thum/") + fileid.split(',')[0];
      if (wh) {//缩略图,默认200*200，可以传宽高/200/200
        if (wh.substring(0, 1) == "/") {
          url += wh;
        } else {
          url += "/" + wh;
        }

      }
      return url;
    }
    else {
      return "";
    }
  },
  userLogin:function(){
    wx.checkSession({
      success:function(){
        //存在登录状态
      },
      fail:function(){
        //不存在登录状态
        ComFunJS.onLogin()
      }
    })
  },
  onLogin:function(){
    wx.login({
      success:function(res){
        if(res.code){
          wx.request({
            url: '',
            data:{
              code:res.code
            },
            success:function(res){
              const _this=this;
              if(res){
                var json=JSON.parse(res.data.Date);
                wx.setStorage({
                  key: 'third_Session',
                  data: 'josn.third_Session',
                })
                ComFunJS.getuserInfo()
              }else{

              }
            },
            fail:function(){

            }
          })
        }
      },
      fail:function(){

      }
    })
  },
  getUserInfo:function(){
    wx.getUserInfo({
      success:function(userInfores){
        var userinfo=res.userinfo;
        ComFunJS.userInfoSetInsql(userInfo)
      }
    })
  },
  userInfoSetInsql: function (userInfo){
    wx.getStorage({
      key: 'third_Session',
      success: function(res) {
        wx.request({

          url: 'Our Server ApiUrl',
          data: {
            third_Session: res.data,
            nickName: userInfo.nickName,
            avatarUrl: userInfo.avatarUrl,
            gender: userInfo.gender,
            province: userInfo.province,
            city: userInfo.city,
            country: userInfo.country
          },
          success: function (res) {
            if (逻辑成功) {
              //SQL更新用户数据成功
            }
            else {
              //SQL更新用户数据失败
            }
          }
        })
      },
    })
  }

}
module.exports = ComFunJS;

